class Compiler {
    constructor(vm) {
        this.el = vm.$el
        this.vm = vm
        this.compiler(this.el)
    }
    // 编译模版，处理文本节点和元素节点
    compiler(el) {
        let childNodes = el.childNodes
        Array.from(childNodes).forEach(node => {
            // 处理文本节点
            if (this.isTextNode(node)) {
                this.compilerText(node)
            } else if (this.isElementNode(node)) {
                // 处理元素节点
                this.compilerElement(node)
            }

            // 判断node 节点是否有子节点，是否有子节点，如果有子节点，要递归调用 compile
            if (node.childNodes && node.childNodes.length) {
                this.compiler(node)
            }
        })
    }
    // 编译元素节点，处理指令

    compilerElement(node) {
        // 模拟 v-text v-model
        //遍历所有的属性节点
        console.log( Array.from(node.attributes))
        Array.from(node.attributes).forEach(attr => {
            let attrName = attr.name
            if(this.isDirective(attrName)){
                // v-text -->text
                attrName = attrName.substr(2)
                let key = attr.value
                if(attrName.startsWith('on')){
                    const event = attrName.replace('on:','')
                    return this.eventUpdate(node, key, event)
                }
                this.update(node, key, attrName)
            }
        })
        // 判断是否是指令

    }



    update(node, key, attrName){
        let updateFn = this[attrName + 'Updater']
        updateFn && updateFn.call(this, node, this.vm[key], key)
    }

    eventUpdate(node, key, event){
        this.onUpdater(node, key, event)
    }
    // 处理 v-text 指令
    textUpdater(node, value, key){
        node.textContent = value
        new Watcher(this.vm, key, (newValue) => {
            node.textContent = newValue
        })
    }

    // 处理 v-model
    modelUpdater(node, value, key){
        node.value = value
        new Watcher(this.vm, key, (newValue) => {
            node.value = newValue
        })
        // 双向绑定
        node.addEventListener('input', () => {
            this.vm[key] = node.value
        })
    }

    // 处理v-on
    onUpdater(node, key, event) {
        node.addEventListener(event, (e) => this.vm[key](e))
    }
    // 处理v-html
    htmlUpdater(node, value, key){
        node.innerHTML = value
        new Watcher(this.vm, key, (newValue) => {
            node.innerHTML = newValue
        })
    }


    // 编译文本节点，处理差值表达式
    compilerText(node) {
        // {{ msg  }}
        let reg = /\{\{(.+?)\}\}/g
        let value = node.textContent
        if(reg.test(value)){
            let key = RegExp.$1.trim()
            node.textContent = value.replace(reg, this.vm[key])
            
            // 创建watcher 对象
            new Watcher(this.vm, key, (newValue) => {
                node.textContent = newValue
            })
        }
    }

    // 判断元素是否是指令
    isDirective(attrName) {
        return attrName.startsWith('v-')
    }

    // 判断节点是否是文本节点
    isTextNode(node) {
        return node.nodeType === 3
    }
    // 盘点是否是元素节点
    isElementNode(node) {
        return node.nodeType === 1
    }



}
